/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  edb_main.h
 *  scatterd
 *
 *  Created by Grant Jones on 3/1/08.
 *
 */

#ifndef EDB_MAIN_H
#define EDB_MAIN_H

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/signal.h>

/* Required by event.h. */
#include <sys/time.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>

/* Libevent. */
#include <event.h>
#include <evhttp.h>

#include "cmap.h"
#include "strmap.h"
#include "radixtree.h"
#include "radixtree_cache.h"
#include "edb_log.h"


/* HTTP REST API */
#ifndef NO_REST_HTTP_SERVER
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#endif

    /*  0        1         2         3   */
    /*  12345678901234567890123456789012 */
#define EDB_ERR_ALLOCATE				0x0001

#define EDB_ERR_DOMAIN_FILE				0x0020
#define EDB_ERR_DOMAIN_WRITE			0x0030
#define EDB_ERR_DOMAIN_READ				0x0040
#define EDB_ERR_DOMAIN_NEXT_UID			0x0050
#define EDB_ERR_DOMAIN_PUT_FAIL			0x0060



/* response preamble */
#define EDB_RESPONSE_PA					"RESPONSE"

/* response keys */
#define EDB_RESULT_KEY					"result"
#define EDB_ERROR_KEY					"error"
#define EDB_NAME_KEY					"_name"
#define EDB_AFFECT_COUNT_KEY			"affect_count"

/* response values */
#define EDB_RESULT_VALUE_OK				"ok"
#define EDB_RESULT_VALUE_FAILURE		"failure"

#define EDB_ERROR_VALUE_STR_PARSE		"Request parsing failed"



#define PACKAGE			"edb"
#define VERSION			"0.1b"
#define DEFAULT_SHELL_PORT	11911
#define DEFAULT_HTTP_PORT	8080


typedef struct
{
	struct event_base *net_evbase;
	
	struct evhttp *http_srv;
	

	
	rtree_t *rt_db_ptr;
	
	/* net-shell client states: */
	cmapStruct *clients;

	/* strmap */
	cmapStruct *domains;	

	/* string allocator : used for file names */
	mempool_t stralloc;		

	unsigned short net_shell_port;
	struct in_addr net_shell_addr;

	int bGlobalSyncDisabled;

	int bDumpMemOnDC;
	int bDebugConnect;
	int bDumpMemOnReq;

#ifndef NO_REST_HTTP_SERVER
	lua_State *luaVM;
	unsigned short net_http_port;
	const char *net_http_file;
#endif
	
} ProgramContextStruct;

/* generic value and value length container */
typedef struct Token {
  const unsigned char *z; /* Text of the token.  Not NULL-terminated! */
  unsigned n    : 31;     /* Number of characters in this token */
} Token;
typedef struct Token edb_value_t;


int edb_main( ProgramContextStruct *pcs, int argc, char *argv[] );
int edb_shell_main( ProgramContextStruct *pcs, int argc, char * const argv[]);
int edb_main_stop( ProgramContextStruct *pcs );

#endif


